import axios from 'axios'
import config from '@/config.js'
import util from '@/util/util.js'
const URLWEBHTTP = config.URLWEBHTTP
// const appConfigs = config.TIME
/**
 * 封装的axios和ajax 一般用axios足够。如果有jsonp需求可以使用zport的ajax;
 */

export default async (apiurl = '', params = {}, type = 'GET', method = 'axios') => {
    type = type.toUpperCase();
    // params['fromToken'] = 'lfqyy';

    if (method == 'axios') {
        return new Promise((resolve, reject) => {
            axios({
                method: type,
                url: URLWEBHTTP + apiurl,
                headers: {
                    'Content-Type': 'application/json;charset=utf-8'
                    // 'Content-Type': 'application/x-www-form-urlencoded'
                },

                data: ((type == 'POST') || (type == 'PUT')) ? params : '',
                params: ((type == 'GET') || (type == 'DELETE')) ? params : '',
                // timeout: appConfigs,
                /* 开启跨域cookie携带 */
                credentials: true,
                emulateHTTP: true,
                emulateJSON: true
            }).then(res => {
                resolve(res)

                if (res.data.status == -555 || res.data.status == -999) {
                    window.location.href = '/#/login';
                }
            }).catch(err => {
                //util.vueEvent.$emit('disloading')
                reject(err)
            })
        });
    } else {
        return new Promise((resolve, reject) => {
            $.ajax({
                url: 'http://120.52.98.115' + apiurl,
                dataType: 'json',
                data: params,
                xhrFields: {
                    withCredentials: true,
                },
                success: function (res) {
                    resolve(res)
                },
                error: function (res) {
                    reject(err)
                }
                // timeout: 50000
            })
        });
    }
}
